package parseAndHandleTool;

import com.oocl.crm_system.Customer;
import com.oocl.crm_system.DoublyList;
import com.oocl.crm_system.Node;

public class ParseCmdForLook extends ParseCmd{
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 * @return void
	 * @Description look for customer by name or id, sex, birthday, address,call
	 */
	public void parseCmdForLook(DoublyList<Customer> list,String inputByConsole){
		String input = inputByConsole;
		
		if(input.length() == 1){
			list.printList();
			return;
		}
		String[] buf = input.split(" ");
		String buf2 = buf[1];        
		String buf3[] = buf2.split(":");
		String buf1 = buf3[1];
		String tt = buf3[0];
		boolean isFind = false;	
		for(int i = 0; i < list.nodeNum; i++){
			Node<Customer> currentNode = list.findNodeByIndex(i);
			Customer currentcustomer = currentNode.value;
			if(tt.equals("name")){
				if(buf1.equals(currentcustomer.getName())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("sex")){
				if(buf1.equals(currentcustomer.getSex())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("birthday")){
				if(buf1.equals(currentcustomer.getBirthday())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("address")){
				if(buf1.equals(currentcustomer.getAddress())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("id")){
				if(Integer.parseInt(buf1) == currentcustomer.getId()){    //parse int
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else if(tt.equals("call")){
				if(buf1.equals(currentcustomer.getCall())){
					printAllcustomer(currentcustomer);
					isFind = true;
				}
			}
			else{
				
			}	
		}
		
		if(!isFind){
			System.out.println("can't not find persons with this charater");
		}
	}
	
	private void printAllcustomer(Customer customer){
		System.out.println(customer.getId() + "\t" + customer.getName() + "\t" + customer.getSex() + "\t" + 
				customer.getBirthday() + "\t" + customer.getAddress() + "\t" + customer.getCall());
}
}
